package com.ps.controller;


import com.google.code.kaptcha.Producer;
import com.ps.bean.BeiUser;
import com.ps.mapper.BeiUserMapper;
import com.ps.service.BeiUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * <p>
 * 用户表  前端控制器
 * </p>
 *
 * @author 小伍
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/bei-user")
@Api(tags = "用户登录接口")
@Slf4j
public class BeiUserController {


    @Autowired
    private BeiUserService beiUserService;//用户service层


    @Autowired
    private Producer producer; //验证码类




    @GetMapping("/user/{uid}")
    @ApiOperation("根据用户id查询信息")
    public BeiUser queryuserbyuid(@PathVariable("uid") String uid)
    {
       return beiUserService.getById(uid);
    }





    @RequestMapping(path = "/user/kaptcha", method = RequestMethod.GET)
    @ApiOperation("验证码")
    public void getKaptcha(HttpServletResponse response, HttpSession session) {
        // 生成验证码
        String text = producer.createText();
        BufferedImage image = producer.createImage(text);
        // 将验证码存入session
        session.setAttribute("kaptcha", text);
        // 将突图片输出给浏览器
        response.setContentType("image/png");
        try {
            OutputStream os = response.getOutputStream();
            ImageIO.write(image, "png", os);
        } catch (IOException e) {
            log.error("响应验证码失败:{}" ,e.getMessage());
        }
    }

    @PostMapping("/user")
    @ApiOperation("用户登录接口")
    public ResponseEntity<Object> userlogin(BeiUser beiUser){
        System.out.println(22);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }






}

